from django.http import JsonResponse
from django.shortcuts import render, redirect, HttpResponse
from app01 import models
from django.urls import reverse
import os


def login(request):
	print(reverse('usertt'))
	return render(request, 'login.html')


def usertt(request):
	user_list = models.UserName.objects.all()
	return render(request, 'usertt.html', locals())


def add_user(request):
	if request.method == 'GET':
		role_list = models.Role.objects.all()
		return render(request, 'add_user.html', locals())
	else:
		username = request.POST.get('username')
		phone_number = request.POST.get('phone_number')
		sex = request.POST.get('sex')
		ret = models.Gender.objects.create(sex=sex)
		roles = request.POST.getlist('roles')
		user = models.UserName.objects.create(username=username, phone_number=phone_number, ad=ret)
		user.roles.add(*roles)
		return redirect('usertt')


def edit_user(request, pk):
	if request.method == 'GET':
		role_list = models.Role.objects.all()
		edit_user = models.UserName.objects.get(id=pk)

		role_lst = []
		for i in edit_user.roles.all():
			role_lst.append(i.id)

		return render(request, "edit_user.html", locals())
	else:
		username = request.POST.get('username')
		phone_number = request.POST.get('phone_number')
		sex = request.POST.get('sex')
		ret = models.Gender.objects.filter(id=pk).update(sex=sex)
		roles = request.POST.getlist('roles')
		models.UserName.objects.filter(id=pk).update(username=username, phone_number=phone_number)
		user = models.UserName.objects.get(id=pk)
		user.roles.set(roles)
		return redirect("usertt")


def ajax_del(request, u_id):
	try:
		models.UserName.objects.get(id=u_id).delete()
		res_data = {'status': 1, 'msg': '删除成功!'}
	except:
		res_data = {'status': 0, 'msg': '删除失败!'}

	return JsonResponse(res_data)

def sousuo(request):
	sousuo = request.GET.get('sousuo')

	if sousuo.isdecimal():
		data = models.UserName.objects.filter(telephone=sousuo)
	elif sousuo in ['男性', '女性', '男', '女']:
		if '男' in sousuo:
			data = models.UserName.objects.filter(sex=1)
		else:
			data = models.UserName.objects.filter(sex=0)
	else:
		data = models.UserName.objects.filter(user=sousuo)
	caidan_dict = [{'url': '/usertt/', 'value': '用户管理'}, {'url': '/role/', 'value': '角色管理'}]

	return render(request, 'usertt.html', locals())


def upload(request):
	up_file = request.FILES.get('ggg')
	with open(f'./statics/jpg/{up_file.name}', 'wb')as g:
		for v in up_file.chunks():
			g.write(v)
	path = '/static/jpg/' + up_file.name
	user = request.session.get('user')
	print(user, path)
	models.Role.objects.filter(username=user).update(
		photo=path,
	)
	request.session['photo'] = path

	return HttpResponse(path)


def sousuo(request):
	sousuo = request.GET.get('sousuo')

	if sousuo.isdecimal():
		user_list = models.UserName.objects.filter(phone_number=sousuo)
	elif sousuo in ['男性', '女性', '男', '女']:
		if '男' in sousuo:
			user_list = models.UserName.objects.filter(ad__sex=1)
		else:
			user_list = models.UserName.objects.filter(ad__sex=0)
	else:
		user_list = models.UserName.objects.filter(username__contains=sousuo)

	caidan_dict = [{'url': '/usertt/', 'value': '返回首页'}]

	return render(request, 'usertt.html', locals())
